package io.youngledo.usb;

import io.youngledo.usb.storage.StorageObject;

/**
 * 文件类型图标管理器
 * 基于文件扩展名返回对应的Unicode图标
 */
public class FileIconManager {

    // 文件夹图标
    public static final String FOLDER_ICON = "📁";
    public static final String FOLDER_OPEN_ICON = "📂";

    // 默认文件图标
    public static final String DEFAULT_FILE_ICON = "📄";

    // 文档类图标
    public static final String DOCUMENT_ICON = "📄";
    public static final String PDF_ICON = "📕";
    public static final String WORD_ICON = "📘";
    public static final String EXCEL_ICON = "📗";
    public static final String POWERPOINT_ICON = "📙";
    public static final String TEXT_ICON = "📃";

    // 图片类图标
    public static final String IMAGE_ICON = "🖼️";
    public static final String PHOTO_ICON = "📷";

    // 音视频类图标
    public static final String AUDIO_ICON = "🎵";
    public static final String VIDEO_ICON = "🎬";

    // 压缩文件图标
    public static final String ARCHIVE_ICON = "🗃️";
    public static final String ZIP_ICON = "🗜️";

    // 代码文件图标
    public static final String CODE_ICON = "💻";
    public static final String JSON_ICON = "🔧";
    public static final String XML_ICON = "🔧";

    // 数据库文件图标
    public static final String DATABASE_ICON = "🗄️";

    // 系统文件图标
    public static final String EXECUTABLE_ICON = "⚙️";
    public static final String SETTINGS_ICON = "⚙️";

    /**
     * 根据StorageObject获取对应的图标
     */
    public static String getFileIcon(StorageObject object) {
        if (object == null) {
            return DEFAULT_FILE_ICON;
        }

        // 如果是目录，返回文件夹图标
        if (object.isDirectory()) {
            return FOLDER_ICON;
        }

        // 根据文件扩展名返回图标
        String extension = object.getExtension().toLowerCase();
        return getIconByExtension(extension);
    }

    /**
     * 根据文件扩展名获取图标
     */
    public static String getIconByExtension(String extension) {
        if (extension == null || extension.isEmpty()) {
            return DEFAULT_FILE_ICON;
        }

        return switch (extension) {
            // 文档类型
            case "pdf" -> PDF_ICON;
            case "doc", "docx" -> WORD_ICON;
            case "xls", "xlsx" -> EXCEL_ICON;
            case "ppt", "pptx" -> POWERPOINT_ICON;
            case "txt", "md", "readme" -> TEXT_ICON;

            // 图片类型
            case "jpg", "jpeg", "png", "gif", "bmp", "webp", "svg" -> IMAGE_ICON;
            case "raw", "tiff", "tif" -> PHOTO_ICON;

            // 音频类型
            case "mp3", "wav", "flac", "aac", "ogg", "m4a", "wma" -> AUDIO_ICON;

            // 视频类型
            case "mp4", "avi", "mkv", "mov", "wmv", "flv", "webm", "m4v" -> VIDEO_ICON;

            // 压缩文件类型
            case "zip", "rar", "7z", "tar", "gz" -> ZIP_ICON;
            case "bz2", "xz", "z" -> ARCHIVE_ICON;

            // 代码文件类型
            case "java", "js", "py", "cpp", "c", "h", "cs", "php", "rb", "go" -> CODE_ICON;
            case "html", "htm", "css", "scss", "less" -> CODE_ICON;
            case "json", "yaml", "yml", "toml" -> JSON_ICON;
            case "xml", "xsl", "xsd" -> XML_ICON;

            // 数据库文件
            case "db", "sqlite", "sqlite3", "mdb" -> DATABASE_ICON;

            // 可执行文件
            case "exe", "msi", "usb", "deb", "rpm" -> EXECUTABLE_ICON;
            case "bin", "run", "command" -> EXECUTABLE_ICON;

            // 配置文件
            case "ini", "cfg", "conf", "config", "properties" -> SETTINGS_ICON;

            // 默认图标
            default -> DEFAULT_FILE_ICON;
        };
    }

    /**
     * 获取文件类型的显示名称
     */
    public static String getFileTypeName(StorageObject object) {
        if (object == null) {
            return "文件";
        }

        if (object.isDirectory()) {
            return "文件夹";
        }

        String extension = object.getExtension().toLowerCase();
        if (extension.isEmpty()) {
            return "文件";
        }

        return switch (extension) {
            case "pdf" -> "PDF文档";
            case "doc", "docx" -> "Word文档";
            case "xls", "xlsx" -> "Excel表格";
            case "ppt", "pptx" -> "PowerPoint演示文稿";
            case "txt" -> "文本文件";
            case "md" -> "Markdown文档";

            case "jpg", "jpeg" -> "JPEG图片";
            case "png" -> "PNG图片";
            case "gif" -> "GIF图片";
            case "svg" -> "SVG矢量图";

            case "mp3" -> "MP3音频";
            case "wav" -> "WAV音频";
            case "flac" -> "FLAC音频";

            case "mp4" -> "MP4视频";
            case "avi" -> "AVI视频";
            case "mkv" -> "MKV视频";

            case "zip" -> "ZIP压缩包";
            case "rar" -> "RAR压缩包";
            case "7z" -> "7Z压缩包";

            case "java" -> "Java源码";
            case "js" -> "JavaScript";
            case "py" -> "Python脚本";
            case "json" -> "JSON数据";
            case "xml" -> "XML文档";

            default -> extension.toUpperCase() + "文件";
        };
    }

    /**
     * 检查是否为常见的图片格式
     */
    public static boolean isImageFile(String extension) {
        return switch (extension.toLowerCase()) {
            case "jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "raw", "tiff", "tif" -> true;
            default -> false;
        };
    }

    /**
     * 检查是否为常见的视频格式
     */
    public static boolean isVideoFile(String extension) {
        return switch (extension.toLowerCase()) {
            case "mp4", "avi", "mkv", "mov", "wmv", "flv", "webm", "m4v" -> true;
            default -> false;
        };
    }

    /**
     * 检查是否为常见的音频格式
     */
    public static boolean isAudioFile(String extension) {
        return switch (extension.toLowerCase()) {
            case "mp3", "wav", "flac", "aac", "ogg", "m4a", "wma" -> true;
            default -> false;
        };
    }
}